﻿using Auto.AlgsAndPush.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Auto.AlgsAndPush.Services.ResultFilter
{
    public class TimeCalculator
    {
        /// <summary>
        /// 为了方便时间的比较，将滤波配置时间与传入的数据时间同步到一天
        /// </summary>
        /// <param name="config">滤波配置</param>
        /// <param name="prjDateTime">MQ数据时间</param>
        /// <param name="startTime">滤波配置同步后的开始时间</param>
        /// <param name="endTime">滤波配置同步后的结束时间</param>
        public static void Calculate(AutoSyncConfig config, DateTime prjDateTime, out DateTime startTime, out DateTime endTime)
        {
            var start = config.StartTime.TimeOfDay;
            var end = config.EndTime.TimeOfDay;
            startTime = prjDateTime.Date.Add(start);
            endTime = prjDateTime.Date.Add(end);
            if (start > end)
            {
                startTime = startTime.AddDays(-1);
            }
        }

        /// <summary>
        /// 通过配置的测量计划，查找到滤波时间范围的最后一个测量计划时间
        /// </summary>
        /// <param name="momentConfigs">测量计划</param>
        /// <param name="FilterEndTime">滤波配置的endTime</param>
        /// <param name="lastMoment">返回滤波配置的范围内的最后一期数据的时间</param>
        public static void GetConfigLastMoment(List<DateTime> momentConfigs, DateTime FilterEndTime, out DateTime lastMoment)
        {
            var last = momentConfigs.Where(m => m.TimeOfDay <= FilterEndTime.TimeOfDay)
                .OrderByDescending(m => m.TimeOfDay)
                .Select(m => m.TimeOfDay)
                .FirstOrDefault();
            lastMoment = FilterEndTime.Date.Add(last);
        }

    }
}
